package com.huawei.cloud.phone.management.domain.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Component
public class UserEventPublish {
    @Autowired
    private UserGateway userGateway;

    @Autowired(required = false)
    private List<IUserEventCallback> callbackList;

    @PostConstruct
    public void init() {
        if (Objects.isNull(callbackList) || callbackList.isEmpty()) {
            callbackList = Collections.emptyList();
        }
    }

    public void forceLogout(String username) {
        Optional<String> userId = userGateway.getUserId(username);
        if (!userId.isPresent()) {
            return;
        }
        callbackList.forEach(callback -> callback.forceLogout(userId.get()));
    }

    public void addUser(String userId) {
        callbackList.forEach(callback -> callback.addUser(userId));
    }
}
